<?php

namespace Admin\Controller;

use Admin\Controller\CommonController;
use Usap\Service\Service;

/**
 *设备
 */
class DevicesController extends CommonController
{


    /**
     * 设备状态
     */
    public function status($page = 1, $rows = 20, $sort = 'device_id', $order = 'asc')
    {
        $devices_service = Service::instance()->find('Devices', 'devices');
        $place_service = Service::instance()->find('operation', 'place');
        //获取设备类别配置
        $device_categroy_config = $devices_service->getDeviceCategoryConfig();
        //获取设备型号
        $device_types_config = $devices_service->getDeviceTypesConfig();
        //巡检周期配置
        $check_cycle_config = Service::instance()->loadConfig('Common', 'device', 'getCheckCycle');
        //场所配置
        $place_config = $place_service->get_place_config();
        $status_config = Service::instance()->loadConfig('common', 'device', 'getRunStatusConf');
        if (IS_POST) {
            $where = 1;
            $request = I('request.');
            if ($request['keyword']) {
                $keyword = $request['keyword'];
                $where .= " and codeserial like '%$keyword%'";
            }
            if (isset($request['status']) && $request['status'] >= 0) {
                $status = $request['status'];
                $where .= " and is_online = '$status'";
            }
            $device_list = $devices_service->getDeviceList($where, $page, $rows, $sort, $order);
            $list = [];
            if ($device_list['count'] > 0) {
                foreach ($device_list['list'] as $value) {
                    $res['codeserial'] = $value['codeserial'];
                    $res['alarms_times'] = $value['alarms_times'];
                    $res['last_report_time'] = $value['last_report_time'];
                    $res['last_check_time'] = $value['last_check_time'];
                    $res['alarms_type'] = $value['alarms_type'] ? $value['alarms_type'] : '未知';
                    $res['device_type'] = $device_types_config[$value['device_type']];
                    $res['device_category'] = $device_categroy_config[$value['device_category']];
                    $res['device_address'] = $value['device_address'];
                    $res['check_cycle'] = $check_cycle_config[$value['check_cycle']];
                    $res['is_online'] = $status_config[$value['is_online']];
                    $list[] = $res;
                }
            }
            $data = array('total' => $device_list['count'], 'rows' => $list);
            $this->ajaxReturn($data);
        } else {
            $this->display('status');
        }

    }


    /**
     * 设备列表
     */
    public function manage($page = 1, $rows = 20, $sort = 'device_id', $order = 'asc')
    {
        $devices_service = Service::instance()->find('Devices', 'devices');
        $place_service = Service::instance()->find('operation', 'place');
        //获取设备类别配置
        $device_categroy_config = $devices_service->getDeviceCategoryConfig();
        //获取设备型号
        $device_types_config = $devices_service->getDeviceTypesConfig();
        //巡检周期配置
        $check_cycle_config = Service::instance()->loadConfig('Common', 'device', 'getCheckCycle');
        //场所配置
        $place_config = $place_service->get_place_config();
        //场所性质
        $place_type_config = Service::instance()->loadConfig('common', 'common', 'getPlaceTypeConfig');
        if (IS_POST) {
            $request = I('request.');
            $where = 1;
            if ($request['keyword']) {
                $keyword = $request['keyword'];
                $key_type = $request['key_type'];
                if ($key_type == 1) {
                    $where .= " and codeserial like '%$keyword%'";
                } elseif ($key_type == 2) {
                    $where .= " and device_address like '%$keyword%'";
                } else {
                    $where .= " and ( device_address like '%$keyword%' or codeserial like '%$keyword%' )";
                }
            }
            if ($request['place_id']) {
                $place_id = $request['place_id'];
                $where .= " and place_id = '$place_id'";
            }
            $device_list = $devices_service->getDeviceList($where, $page, $rows, $sort, $order);
            $list = [];
            if ($device_list['count'] > 0) {
                foreach ($device_list['list'] as $value) {
                    $res['device_id'] = $value['device_id'];
                    $res['codeserial'] = $value['codeserial'];
                    $res['device_address'] = $value['device_address'];
                    $res['device_type'] = $device_types_config[$value['device_type']];
                    $res['device_category'] = $device_categroy_config[$value['device_category']];
                    $res['place_id'] = $place_config[$value['place_id']];
                    $res['check_cycle'] = $check_cycle_config[$value['check_cycle']];
                    $res['manager'] = $value['manager'];
                    $res['manager_phone'] = $value['manager_phone'];
                    $res['contacts_phone'] = $value['contacts_phone'];
                    $list[] = $res;
                }
            }
            $data = array('total' => $device_list['count'], 'rows' => $list);
            $this->ajaxReturn($data);
        } else {
            $this->assign('place_config', $place_config);
            $this->assign('place_type_config', $place_type_config);
            $this->display('manage');
        }

    }


    /**
     * 新增/编辑设备信息
     */
    public function set_devices_info()
    {
        $user_id = session('userid');
        $devices_service = Service::instance()->find('Devices', 'devices');
        $place_service = Service::instance()->find('operation', 'place');
        //获取设备类别配置
        $device_categroy_config = $devices_service->getDeviceCategoryConfig();
        //获取设备型号
        $device_types_config = $devices_service->getDeviceTypesConfig();
        //巡检周期配置
        $check_cycle_config = Service::instance()->loadConfig('Common', 'device', 'getCheckCycle');
        //场所配置
        $place_config = $place_service->get_place_config();
        $id = I('request.id');
        $device_info = [];
        if ($id) {
            $device_info = $devices_service->getInfoByDeviceId($id);
        }
        if (IS_POST) {
            $request = I('request.');
            if ($request['device_category'] < 0) $this->ajaxReturn(array('code' => 1, 'msg' => '设备类型不能为空'));
            if ($request['device_type'] < 0) $this->ajaxReturn(array('code' => 1, 'msg' => '设备型号不能为空'));
            if ($request['check_cycle'] < 0) $this->ajaxReturn(array('code' => 1, 'msg' => '巡检周期不能为空'));
            if ($request['place_id'] < 0) $this->ajaxReturn(array('code' => 1, 'msg' => '所属场所不能为空'));
            $data = [
                'name' => $request['name'],
                'codeserial' => $request['codeserial'],
                'device_type' => $request['device_type'],
                'device_category' => $request['device_category'],
                'check_cycle' => $request['check_cycle'],
                'place_id' => $request['place_id'],
                'device_address' => $request['device_address'],
                'manager' => $request['manager'],
                'manager_phone' => $request['manager_phone'],
                'contacts_phone' => $request['contacts_phone'],
                'position' => $request['position'],
                'user_id' => $user_id,
            ];
            if ($request['device_id']) {
                $data['updated_time'] = date('Y-m-d H:i:s');
                $res = $devices_service->updateDevice($request['device_id'], $data);
            } else {
                $res = $devices_service->addDevice($data);
            }
            if ($res['code'] == '0000') {
                $data = ['code' => '0000', 'msg' => '操作成功'];
                $this->ajaxReturn($data);
            } else {
                $this->ajaxReturn($res);
            }

        } else {
            //地图地址
            $map_url = C('API_URL') . '/index.html';
            $this->assign('map_url', $map_url);
            $this->assign('device_categroy_config', $device_categroy_config);
            $this->assign('device_types_config', $device_types_config);
            $this->assign('place_config', $place_config);
            $this->assign('check_cycle_config', $check_cycle_config);
            $this->assign('device_info', $device_info);
            $this->display('set_devices_info');
        }

    }


    /**
     * 删除设备
     */
    public function delete_devices()
    {
        $id = I('request.id');
        $devices_service = Service::instance()->find('Devices', 'devices');
        if (!is_numeric($id)) {
            $id = explode(',', $id);
        }
        if (is_array($id)) {
            foreach ($id as $v) {
                $devices_service->deletDevice($v);
            }
            $this->success('删除成功');
        } else {
            $device_info = $devices_service->getInfoByDeviceId($id);
            if ($device_info) {
                $result = $devices_service->deletDevice($id);
                if ($result['code'] == '0000') {
                    $this->success('删除成功');
                } else {
                    $this->ajaxReturn($result);
                }
            } else {
                $this->error('没有数据或已删除过了，请稍后再试');
            }
        }
    }
}

